package com.gitee.shigen.sample.controller;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.*;

import com.gitee.shigen.api.feign.user.vo.UserVO;
import com.gitee.shigen.common.msg.Result;
import com.gitee.shigen.sample.entity.User;
import com.gitee.shigen.sample.service.IUserService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * @author shigenfu
 * @date 2023/2/25 12:06 上午
 */
@RestController
@RequestMapping(value = "user")
@Tag(name = "userController", description = "对user的操作")
public class UserController {

    @Resource
    private IUserService userService;

    @Operation(summary = "添加记录")
    @PostMapping(value = "add")
    public Result<Integer> createUser(@RequestBody @Valid User user) {
        int result = userService.insert(user);
        return Result.success(result);
    }

    @Operation(summary = "删除记录")
    @DeleteMapping(value = "delete/{id}")
    public Result<Integer> deleteUser(@PathVariable("id") Long id) {
        int result = userService.delete(id);
        return Result.success(result);
    }

    @Operation(summary = "修改记录")
    @PutMapping(value = "update")
    public Result<Integer> updateUser(@RequestBody @Valid User user) {
        int result = userService.update(user);
        return Result.success(result);
    }

    @Operation(summary = "查询记录")
    @GetMapping(value = "get/{id}")
    public Result<UserVO> queryUser(@PathVariable("id") Long id) {
        UserVO userVO = userService.query(id);
        return Result.success(userVO);
    }

    @GetMapping(value = "getByName/{username}")
    public Result<UserVO> loadUserByUsername(@PathVariable("username") String username) {
        UserVO userVO = userService.loadUserByUsername(username);
        return Result.success(userVO);
    }
}
